﻿package org.ilanchong.droidcontroller.ui;

import org.ilanchong.droidcontroller.DroidController;
import org.ilanchong.droidcontroller.DroidControllerActivity;
import org.ilanchong.droidcontroller.BroadCastCommand;
import org.ilanchong.droidcontroller.R;
import org.ilanchong.droidcontroller.Utils;

import android.util.Log;


public class UpdateComputerMessageUI extends
		android.os.AsyncTask<BroadCastCommand, Integer, ComputerResultInfo> {
	private final static String LOG_TAG = "UpdateComputerMessageUI";
	private DroidControllerActivity client = null;

	public UpdateComputerMessageUI(DroidControllerActivity _androidClient) {
		this.client = _androidClient;
	}

	@Override
	protected void onPreExecute() {
		client.getStatusView().setText(R.string.getting_computer);
	}

	@Override
	protected ComputerResultInfo doInBackground(BroadCastCommand ... params) {
		BroadCastCommand arg = params[0];
		String localIp = Utils.getLocalIP(client);
		return DroidController.getIPHostname(arg.getBroadCastIP(), localIp, arg.getPort(), arg.getCommand());
	}

	@Override
	protected void onPostExecute(ComputerResultInfo result) {
		if(result.getResult() == false){
			this.client.getStatusView().setText("Error:" + result.getMessage());
			Log.e(LOG_TAG, "Error:" + result.getMessage());
		}else {	
			this.client.getStatusView().setText("Computer:" + result.getComputer().getHostName() + ":" + result.getComputer().getIP());
			Log.d(LOG_TAG, "Computer:" + result.getComputer().getHostName() + ":" + result.getComputer().getIP());
		}
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
	}
}
